package com.chaidb;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.db.DbConnection;

public class GetPhotoURL extends HttpServlet {

	String markername = null;
	String userid = null;

	/**
	 * Constructor of the object.
	 */
	public GetPhotoURL() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String url = null;
		String markerid = null;
		response.setContentType("application/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("GB18030");// 防止乱码
		HttpSession session = request.getSession();
		markername = request.getParameter("markername");
		userid = request.getParameter("userid");
		String sql2 = "select * from marker where markername='" +  markername
		+ "'&&userid='" + userid +"';";
		
		System.out.println(sql2);
		Connection con = null;
		try {
			con = DbConnection.getInstance().getConnection();
			Statement st2 = con.createStatement();
			ResultSet query2 =st2.executeQuery(sql2);
			if(query2.next())
			{
				markerid = query2.getString("id");
				String sql = "select * from photos where markerid='" 
					+ markerid +"';";
				System.out.println(sql);
				Statement st = con.createStatement();
				ResultSet query =st.executeQuery(sql);
				if(query.next())
				{
					url = query.getString("photourl");
				}
				
				
			}
		}catch(SQLException e)
		{
			e.printStackTrace();
		}finally{
			out.println("<URL>"+ url + "</URL>");
		}
	}
		

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
